import Vec2 = cc.Vec2;

const {ccclass, property} = cc._decorator;
// 玩家的基类
@ccclass
export default class Player extends cc.Component {


    @property(cc.Sprite)
    bird: cc.Sprite = null;
    @property(cc.Sprite)
    bird1: cc.Sprite = null;
    @property(cc.Node)
    parentNode: cc.Node = null;
    @property(cc.Prefab)
    bullet: cc.Prefab = null;
    healthPoint:number = 1;
    timer: number = 0;
    shootSpeed: number = 0.5;  // 发射子弹的速度

    update(dt: number) {
        this.timer += dt;
        this.shootBullet();

    }

    // 机体发射子弹
    shootBullet(){
        if (this.timer - this.shootSpeed > 0) {
            this.timer = 0;
            var loadobj =cc.instantiate(this.bullet);//将预制体克隆到场景
            this.node.parent.addChild(loadobj);//将克隆出的物体作为子物体
            loadobj.setPosition(cc.v2(this.node.x,this.node.y));
        }
    }

// ——————————————————————————————————————————————————————————————————————
    /**
     * 随触点移动
     */
    nodePos: Vec2 = null;
    // this.node 是需要移动的节点
    onLoad () {
        //节点初始位置,每次触摸结束更新
        this.nodePos = this.node.getPosition();
        //触摸监听(this.node.parent是屏幕)
        //想达到按住节点，节点才能移动的效果，将监听函数注册到 this.node 上，去掉  .parent 即可
        this.node.parent.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.parent.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.parent.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
    }

    //触摸移动；
    onTouchMove (event) {
        let self = this;
        let touches = event.getTouches();
        //触摸刚开始的位置
        let oldPos = self.node.parent.convertToNodeSpaceAR(touches[0].getStartLocation());
        //触摸时不断变更的位置
        let newPos = self.node.parent.convertToNodeSpaceAR(touches[0].getLocation());

        //var subPos = cc.pSub(oldPos,newPos); 1.X版本是cc.pSub

        let subPos = oldPos.sub(newPos); // 2.X版本是 p1.sub(p2);

        self.node.x = self.nodePos.x - subPos.x;
        self.node.y = self.nodePos.y - subPos.y;

        // 控制节点移不出屏幕;
        let minX = -self.node.parent.width/2 + self.node.width/2; //最小X坐标；
        let maxX = Math.abs(minX);
        let minY = -self.node.parent.height/2 + self.node.height/2; //最小Y坐标；
        let maxY = Math.abs(minY);
        let nPos = self.node.getPosition(); //节点实时坐标；

        if (nPos.x < minX) {
            nPos.x = minX;
        };
        if (nPos.x > maxX) {
            nPos.x = maxX;
        };
        if (nPos.y < minY) {
            nPos.y = minY;
        };
        if (nPos.y > maxY) {
            nPos.y = maxY;
        };
        self.node.setPosition(nPos);
    }
    onTouchEnd () {
        this.nodePos = this.node.getPosition(); //获取触摸结束之后的node坐标；
    }
    onTouchCancel() {
        this.nodePos = this.node.getPosition(); //获取触摸结束之后的node坐标；
    }
// ——————————————————————————————————————————————————————————————————————

    // 受伤减少生命值
    beInjured(damage: number) {
        console.log("自己受伤害了");
        this.healthPoint -= damage;
        if (this.healthPoint <= 0) {
            this.node.destroy();
        }
    }
}
